﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Assets.Script.Config;

public class DriverInfo : MonoBehaviour {
    public bool m_bLoadTxtJson = false;

    public static GameObject SingletonObj;
    public List<HeroVo> heros;
    public List<Action_Attack> m_Attack;
    public List<Action_Sing> m_Sing;
    public List<Action_DoBuffer> m_DoBuf;
    public List<ActionNode> m_All;

    public Dictionary<int, Action_Attack> m_FanJi;
    [HideInInspector]
    public List<int> m_IndexFanJi = new List<int>();
    [HideInInspector]
    public List<Action_Attack> m_FanJiData = new List<Action_Attack>();

    public int fanjiKey = HeroFightDef.c_iEditorFanJiNodeStart;


    public float speed = 1;
    private void Start()
    {
        if (SingletonObj == null)
            SingletonObj = gameObject;

        if(m_FanJi == null)
            m_FanJi = new Dictionary<int, Action_Attack>();

        m_FanJi.Clear();
        for (int i = 0; i < m_IndexFanJi.Count; i++)
        {
            if(i< m_FanJiData.Count)
            {
                m_FanJi.Add(m_IndexFanJi[i], m_FanJiData[i]);
            }
        }

        HeroBattleMgr.CreateInstance(true);
        //HeroFightConfig.InitHeroBattleConfig();
        //HeroBattleMgr.CreateInstance(true);
        //HeroBattleMgr.Instance.Init();
        QueueLoadMgr.Instance.Init();
        QueueLoadMgr.Instance.AddLoadTask(ConfigManager.Instance.unitBaseLoader);
        QueueLoadMgr.Instance.AddLoadTask(ConfigManager.Instance.skillActionLoader);
        QueueLoadMgr.Instance.AddLoadTask(ConfigManager.Instance.heroEffLoader);
        QueueLoadMgr.Instance.AddLoadTask(ConfigManager.Instance.SkillLoader);
        QueueLoadMgr.Instance.AddLoadTask(ConfigManager.Instance.SkillEffectLoader);
        QueueLoadMgr.Instance.AddLoadTask(ConfigManager.Instance.LanSetting);
        QueueLoadMgr.Instance.AddLoadTask(ConfigManager.Instance.LangLoader);
        QueueLoadMgr.Instance.onLoadConfigDoneComplete += ConfigDone;
        QueueLoadMgr.Instance.StartLoad();
    }

    void Update()
    {
        QueueLoadMgr.Instance.Update();

        if (Input.GetKeyDown(KeyCode.Q))
        {
            HeroBattleMgr.Instance.PlayNowActionNode();
        }
    }

    public void Click_Speed()
    {
        HeroBattleMgr.Instance.Speed = speed;
    }

    public void ConfigDone()
    {
        if (m_bLoadTxtJson)
        {
            string json = Resources.Load("HeroFight/HeroBattleJson_5").ToString();
            Debug.Log(json);
            ReportDetail report = ConfigManager.ConvertReportDetail(json);
            PrintNowJson(report);
            List<HeroVo> _heros = new List<HeroVo>();
            List<ActionNode> _actions = new List<ActionNode>();
            HBDataCenter.Instance.Report2Fight(_heros, _actions, report,true);
            HeroBattleMgr.Instance.Init(_heros, _actions, null);
            
        }
        else
        {
            HBDataCenter.Instance.Editor2Fight(heros, m_Attack, m_DoBuf, m_Sing, m_FanJi, m_All);
            HeroBattleMgr.Instance.Init(heros, m_All, null);
        }
    }

    private void PrintNowJson(ReportDetail report)
    {
        if(report != null)
        {
            string str = "";
            for (int i = 0; i < report.fight_info.Count; i++)
            {
                List<string> values = new List<string>();
                for (int j = 0; j < report.fight_info[i].values.Count; j++)
                {
                    values.Add(report.fight_info[i].values[j].ToString());
                }
                str += ConfigManager.Instance.LangLoader.GetLang("zb_new_" + report.fight_info[i].action.ToString()
                    , values.ToArray()) + "\n";
            }
            Debug.Log(str);
        }
    }
}
